package cn.chenxiejia;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: T9
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/2/14 19:06
 */

public class T9 {
    public static void main(String[] args) {
        //建立方法一 实现Runnable接口，并作为Thread类参数
        Runnable target = new T11();
        Thread thread1 = new Thread(target);
        thread1.start();
        //建立方法二 继承Thread 类，重写run方法
        Thread thread2 = new T12();
        thread2.start();
        //建立方式三 实现Callable
        //执行 Callable 方式,需要 FutureTask 实现类的支持
        // FutureTask 实现类用于接收运算结果, FutureTask 是 Future 接口的实现类
        T13 t13 = new T13();
        FutureTask<Integer> futureTask = new FutureTask<>(t13);
        new Thread(futureTask).start();
        //futureTask用于接收结果
        try {
            // 只有当 Thread 线程执行完成后,才会打印结果;
            // 因此, FutureTask 也可用于闭锁
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class T11 implements Runnable {

    @Override
    public void run() {
        System.out.println("方式1：实现Runnable接口");
    }
}

class T12 extends Thread {
    @Override
    public void run() {
        System.out.println("方式2：继承Thread类");
    }
}

class T13 implements Callable {

    @Override
    public Object call() throws Exception {
        System.out.println("方式3:实现Callable接口");
        return null;
    }
}
